package com.zzedu.system.service.impl;

import com.zzedu.system.service.ITransactionHandlerService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.function.Supplier;

/**
 * 解决同service中非事务方法调用事务方法事务不生效
 *
 * @author Administrator
 */
@Component
public class TransactionHandlerService implements ITransactionHandlerService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public <T> T runInTransaction(Supplier<T> supplier) {
        return supplier.get();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public <T> T runInNewTransaction(Supplier<T> supplier) {
        return supplier.get();
    }
}
